﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static PvfUtilityDemo.HttpConn;

namespace PvfUtilityDemo 
{
    public class Result
    {
        public string Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }
    public class ResultList
    {
        public string[] Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }
    public class ResultListM
    {
        public Dictionary<string, Dictionary<string, string>> Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }
    public class PostStr
    {
        public List<string> FileList { get; set; }
        public bool UseCompatibleDecompiler { get; set; }
    }
    public class UploadStr
    {
        public string FilePath { get; set; }
        public string FileContent { get; set; }
    }

    public class ItemInfo
    {
        public string ItemName { get; set; }
        public int ItemCode { get; set; } 
    }

    public class ResultItem
    {
        public ItemInfo Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }

    public class ResultItems
    {
        public Dictionary<string,ItemInfo> Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }

    public class ResultListC
    {
        public Dictionary<string, Dictionary<int, string>> Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }

    public class FileInfo
    {
        public string FilePath { get; set; }

        public string ItemName { get; set; }
    }

    public class ResultFileInfo
    {
        public FileInfo Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }
    public class PostInfo
    {
        public List<string> lstNames { get; set; } 
        public List<int> ItemCodes { get; set; } 
    }

    public class ResultFileInfos
    {
        public Dictionary<string,Dictionary<string, FileInfo>> Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
    }

    public class SearchStr
    {
        /// <summary>
        /// 搜索目录
        /// </summary>
        public string SearchFolder { get; set; }
        /// <summary>
        /// 搜索关键字
        /// </summary>
        public string Keyword { get; set; }
        /// <summary>
        /// 搜索类型 整数？字符串？名称？脚本文件内容？
        /// </summary>
        public int Type { get; set; }
        /// <summary>
        /// 搜索源类型 0=全部文件 1=在搜索结果删除 2=在搜索结果查找
        /// </summary>
        public int SourceType { get; set; }
        /// <summary>
        /// 搜索时 是否模糊搜索 是否使用正则
        /// </summary>
        public int NormalUsing { get; set; }
        /// <summary>
        /// 开头匹配
        /// </summary>
        public bool IsStartMatch { get; set; }
        /// <summary>
        /// 要进行筛选的搜索结果集合 如果是从查找结果二次搜索 请在此处传入要搜索的文件路径集合
        /// </summary>
        public string SearchResult { get; set; }
        [JsonIgnore]
        /// <summary>
        /// 
        /// </summary>
        public int ScriptContentSearchMode { get; set; }
        /// <summary>
        /// 搜索路径
        /// </summary>
        public bool IsUseLikeSearchPath { get; set; }
        /// <summary>
        /// 特征搜索
        /// </summary>
        public bool Trait { get; set; }
        /// <summary>
        /// 使用正则表达式
        /// </summary>
        public bool UseRegularExpression { get; set; }
        /// <summary>
        /// 全词匹配
        /// </summary>
        public bool WholeWordMatch { get; set; }
        /// <summary>
        /// 文件类型排除还是保留
        /// </summary>
        public int RemoveOrKeep { get; set; }
        /// <summary>
        /// 要保留或排除的文件类型集合 .equ .ani 如无需使用保持NULL
        /// </summary>
        public string FileTypesString { get; set; }
        /// <summary>
        /// 脚本文件搜索内容
        /// </summary>
        public string ScriptContent { get; set; }
        /// <summary>
        /// 特征搜索 开始内容
        /// </summary>
        public string ScriptContentStart { get; set; }
        /// <summary>
        /// 特征搜索 结束内容
        /// </summary>
        public string ScriptContentStop { get; set; }
    }

    public class ResultLstInfo 
    {
        public Dictionary<int,LstFileInfo> Data { get; set; }
        public bool IsError { get; set; }
        public string Msg { get; set; }
        public int ErrorId { get; set; }

    }
    public class LstFileInfo
    {
        /// <summary>
        /// 类型
        /// </summary>
        public string PathHeader { get; set; }
        /// <summary>
        /// 路径
        /// </summary>
        public string ItemPath { get; set; }
        /// <summary>
        /// 完整路径
        /// </summary>
        public string FullPath { get; set; }
        /// <summary>
        /// 物品名称
        /// </summary>
        public string ItemName { get; set; }
        /// <summary>
        /// 物品代码
        /// </summary>
        public int ItemCode { get; set; }

    }

    public class PvfUtilityConn : HttpClientConn
    {

        public static readonly string httpConn = "http://localhost:";
        
        /// <summary>
        /// 获取pvfUtility版本号
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static Result GetVersion(int port)
        {

            Result result = new Result();
            try
            {
                string url =  $"{httpConn}{port}/Api/PvfUtiltiy/getVersion";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="port"></param>
        /// <param name="dirName"></param>
        /// <param name="fileType"></param>
        /// <returns></returns>
        public static ResultList GetFileList(int port, string dirName, string fileType)
        {
            ResultList result = new ResultList();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetFileList?dirName={dirName}&returnType=0&fileType={fileType}";
                result = JsonConvert.DeserializeObject<ResultList>(GetResponse(url));
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 获取Pvf根目录列表
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static ResultList GetPvfRootDirectory(int port)
        {
            ResultList resultList = new ResultList();
            try
            {
                string url =  $"{httpConn}{port}/Api/PvfUtiltiy/getPvfRootDirectory";
                resultList = JsonConvert.DeserializeObject<ResultList>(GetResponse(url));
            }
            catch (Exception)
            {
                resultList = null;
            }
            return resultList;
        }
        /// <summary>
        /// 获取文件内容
        /// </summary>
        /// <param name="port"></param>
        /// <param name="filePath"></param>
        /// <param name="useCompatibleDecompiler"></param>
        /// <returns></returns>
        public static Result GetFileContent(int port, string filePath, bool useCompatibleDecompiler)
        {
            Result result = new Result();
            try
            {
                string url =  $"{httpConn}{port}/Api/PvfUtiltiy/GetFileContent?filePath={filePath}&useCompatibleDecompiler={useCompatibleDecompiler}";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 批量获取文件内容
        /// </summary>
        /// <param name="port"></param>
        /// <param name="postStr"></param>
        /// <returns></returns>
        public static ResultListM GetFileContents(int port, PostStr postStr)
        {
            ResultListM resultListM = new ResultListM();
            try
            {
                string url =  $"{httpConn}{port}/Api/PvfUtiltiy/GetFileContents";
                string str = JsonConvert.SerializeObject(postStr);
                resultListM = JsonConvert.DeserializeObject<ResultListM>(PostResponse(url, str));

            }
            catch (Exception)
            {
                resultListM = null;
            }

            return resultListM;
        }
        /// <summary>
        /// 获取主要的lst文件列表
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static ResultList GetAllListFileList(int port)
        {
            ResultList result = new ResultList();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetAllLstFileList";
                result = JsonConvert.DeserializeObject<ResultList>(GetResponse(url));
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="port"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool DeleteFile(int port, string filePath)
        {
            bool result = false;
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/DeleteFile?filePath={filePath}";
                string jsonPath = GetResponse(url);
                var resultData = JsonConvert.DeserializeObject<Result>(jsonPath);
                if (resultData.IsError == false)
                {
                    result = true;
                    return result;
                }
                else if (resultData.IsError == true)
                {
                    result = false;
                    return result;
                }
            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }
        /// <summary>
        /// 批量删除文件
        /// </summary>
        /// <param name="port"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static ResultList DeleteFiles(int port, List<string> list)
        {
            ResultList resultList = new ResultList();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/DeleteFiles";
                string postStr = JsonConvert.SerializeObject(list);
                string jsonPath = PostResponse(url, postStr);
                resultList = JsonConvert.DeserializeObject<ResultList>(jsonPath);
            }
            catch (Exception)
            {
                resultList = null;
            }
            return resultList;
        }
        /// <summary>
        /// 上传新的文件内容
        /// </summary>
        /// <param name="port"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool ImportFile(int port, string filePath,string str)
        {
            bool result = false;

            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/ImportFile?filePath={filePath}";
                string jsonPath = PostResponse(url, str);
                var resultData = JsonConvert.DeserializeObject<Result>(jsonPath);
                if (resultData.IsError == false)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }
        /// <summary>
        /// 批量上传新的文件内容
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static bool ImportFiles(int port, List<UploadStr> list)
        {
            bool result = false;

            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/ImportFiles";
                string postStr = JsonConvert.SerializeObject(list);
                string jsonPath = PostResponse(url, postStr);
                var resultData = JsonConvert.DeserializeObject<ResultList>(jsonPath);
                if (resultData.IsError == false)
                    result = true;
                else
                    result = false;

            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }

        /// <summary>
        /// 获取物品信息
        /// </summary>
        /// <param name="port"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static ResultItem GetItemInfo(int port,string filePath)
        {
            ResultItem resultItem = new ResultItem();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetItemInfo?filePath={filePath}";
                resultItem = JsonConvert.DeserializeObject<ResultItem>(GetResponse(url));
            }
            catch(Exception)
            {
                resultItem = null;
            }

            return resultItem;
        }
        /// <summary>
        /// 批量获取物品信息
        /// </summary>
        /// <param name="port"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static ResultItems GetItemInfos(int port,List<string> list)
        {
            ResultItems resultItems = new ResultItems();    
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetItemInfos";
                string jsonPath = JsonConvert.SerializeObject(list);
                resultItems = JsonConvert.DeserializeObject<ResultItems>(PostResponse(url, jsonPath)); 
            }
            catch(Exception)
            {
                resultItems = null;
            }
            return resultItems;
        }
        /// <summary>
        /// 获取文件资源管理器具有焦点的文件路径
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static Result GetTreeListFocusedFilePath(int port)
        {
            Result result = new Result();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetTreeListFocusedFilePath";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));

            }
            catch(Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 获取查找面板文件资源管理器具有焦点的文件路径
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static Result GetSearchPanelTreeListFocusedFilePath(int port)
        {
            Result result = new Result();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetSearchPanelTreeListFocusedFilePath";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
            }
            catch(Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 获取文档组 => 当前正在编辑的文档 => 文件路径
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static Result GetActiveDocumentFilePath(int port)
        {
            Result result = new Result();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetActiveDocumentFilePath";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
            }
            catch(Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 文件路径集合 转换为 lst 返回字典集合
        /// </summary>
        /// <param name="port"></param>
        public static ResultListC FileListToLstRows(int port,List<string> list)
        {
            ResultListC resultListC = new ResultListC();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/FileListToLstRows";
                string jsonPath = JsonConvert.SerializeObject(list);
                resultListC = JsonConvert.DeserializeObject<ResultListC>(PostResponse(url, jsonPath));
            }
            catch(Exception)
            {
                resultListC = null;

            }

            return resultListC;
        }
        /// <summary>
        /// 确认文件是否存在
        /// </summary>
        /// <param name="port"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool FileIsExists(int port,string filePath)
        {
            bool r = false;   
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/FileIsExists?filePath={filePath}";
                var result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
                if(result.IsError==false)
                    r = true;
            }
            catch(Exception)
            {
                r = false;
            }
            return r;
        }
        /// <summary>
        /// 获取文件资源管理器选中文件集合
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static ResultList GetTreeSelectedFiles(int port)
        {
            ResultList resultList = new ResultList();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetTreeSelectedFiles";
                resultList = JsonConvert.DeserializeObject<ResultList>(GetResponse(url));
            }
            catch(Exception)
            {
                resultList = null;
            }
            return resultList;
        }
        /// <summary>
        /// 获取查找面板文件资源管理器选中文件集合
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static ResultList GetSearchPanelSelectedFiles(int port)
        {
            ResultList resultList = new ResultList();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetSearchPanelSelectedFiles";
                resultList = JsonConvert.DeserializeObject<ResultList>(GetResponse(url));
            }
            catch (Exception)
            {
                resultList = null;
            }
            return resultList;
        }
        /// <summary>
        /// pvf封包另存为
        /// </summary>
        /// <param name="port"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Result SaveAsPvfFile(int port,string filePath)
        {
            Result result = new Result();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/SaveAsPvfFile?filePath={filePath}";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
            }
            catch(Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 获取当前载入的封包文件路径
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static Result GetPvfPackFilePath(int port)
        {
            Result result = new Result();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/GetPvfPackFilePath";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 搜索PVF
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static ResultList SearchPvf(int port,SearchStr searchStr)
        {
            ResultList resultList = new ResultList();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/SearchPvf";
                string jsonPath = JsonConvert.SerializeObject(searchStr);
                resultList = JsonConvert.DeserializeObject<ResultList>(PostResponse(url, jsonPath));
            }
            catch(Exception)
            {
                resultList = null;
            }
            return resultList;
        }
        /// <summary>
        /// 使用物品代码转换文件信息
        /// </summary>
        /// <param name="port"></param>
        /// <param name="lstNames"></param>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        public static ResultFileInfo ItemCodeToFileInfo(int port,string lstNames,int itemCode)
        {
            ResultFileInfo resultFileInfo = new ResultFileInfo();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/ItemCodeToFileInfo?lstNames={lstNames}&itemCode={itemCode}";
                resultFileInfo = JsonConvert.DeserializeObject<ResultFileInfo>(GetResponse(url));   
            }
            catch(Exception)
            {
                resultFileInfo = null;
            }
            return resultFileInfo;
        }
        /// <summary>
        /// 批量使用物品代码转换文件信息(返回：文件路径/物品名称)
        /// </summary>
        /// <param name="port"></param>
        public static ResultFileInfos ItemCodesToFileInfos(int port,PostInfo postInfo)
        {
            ResultFileInfos resultFileInfos = new ResultFileInfos();
            try
            {
                string url = $"{httpConn}{port}/Api/PvfUtiltiy/ItemCodesToFileInfos";
                string jsonPath = JsonConvert.SerializeObject(postInfo);
                resultFileInfos = JsonConvert.DeserializeObject<ResultFileInfos>(PostResponse(url,jsonPath));
            }
            catch(Exception)
            {
                resultFileInfos = null;
            }
            return resultFileInfos;

        }
        /// <summary>
        /// 获取文件icon图标
        /// </summary>
        /// <returns></returns>
        public static Result GetFileIcon(int port, string filePath)
        {
            Result result = new Result();
            try
            {
                string url = httpConn + $"{port}/Api/PvfUtiltiy/getFileIcon?filePath={filePath}";
                result = JsonConvert.DeserializeObject<Result>(GetResponse(url));
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// 获取lst文件信息
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static ResultLstInfo GetLstFileInfo(int port)
        {
            ResultLstInfo resultLstInfo = new ResultLstInfo();
            try
            {
                string url = httpConn + $"{port}/Api/PvfUtiltiy/getLstFileInfo?filePath=equipment/equipment.lst";
                resultLstInfo = JsonConvert.DeserializeObject<ResultLstInfo>(GetResponse(url));
            }
            catch (Exception)
            {

                resultLstInfo = null;
            }

            return resultLstInfo;
        }

    }


}
